package com.jdbc;

import java.sql.Connection;
import java.util.List;
import java.util.Scanner;

public class Homework {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        if (login(username,password)){
            System.out.println("登录成功");
            User user = getUserByUsername(username);
            if (user.isIs_admin()) {
                adminMenu(scanner);
            } else {
                customerMenu(scanner, user.getId());
            }
        } else {
            System.out.println("登录失败，用户名或密码错误！");
        }

    }

    public static boolean login(String username,String password) {
        List<User> list = JBDCUtils.query(User.class, "select *from user where username=? and password=?", username, password);
        if (list != null && list.size() > 0) {
            return true;

        }
        return  false;
}

    public static void adminMenu(Scanner scanner) {
        while (true) {
            System.out.println("===== 管理员菜单 =====");
            System.out.println("1. 添加宠物");
            System.out.println("2. 更新宠物");
            System.out.println("3. 删除宠物");
            System.out.println("4. 查看所有宠物");
            System.out.println("0. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除缓冲区

            switch (choice) {
                case 1:

                    System.out.print("请输入宠物名称: ");
                    String petName = scanner.nextLine();
                    System.out.print("请输入宠物类型: ");
                    String petType = scanner.nextLine();
                    System.out.print("请输入宠物年龄: ");
                    int age = scanner.nextInt();
                    scanner.nextLine(); // 清除缓冲区
                    System.out.print("请输入宠物性别: ");
                    String gender = scanner.nextLine();
                    addPet(0, petName, petType, age, gender); // 0 表示无主人

                    break;
                case 2:
                    System.out.print("请输入宠物ID: ");
                    int petId = scanner.nextInt();
                    scanner.nextLine(); // 清除缓冲区
                    System.out.print("请输入新的宠物名称: ");
                    petName = scanner.nextLine();
                    System.out.print("请输入新的宠物类型: ");
                    petType = scanner.nextLine();
                    System.out.print("请输入新的宠物年龄: ");
                    age = scanner.nextInt();
                    scanner.nextLine(); // 清除缓冲区
                    System.out.print("请输入新的宠物性别: ");
                    gender = scanner.nextLine();
                    updatePet(petId, petName, petType, age, gender);

                    break;
                case 3:
                    System.out.print("请输入要删除的宠物ID: ");
                    petId = scanner.nextInt();
                    scanner.nextLine(); // 清除缓冲区
                    deletePet(petId);

                    break;
                case 4:
                    getAllPets();

                    break;
                case 0:
                    return;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }
    public static void customerMenu(Scanner scanner, int userId) {
        while (true) {
            System.out.println("===== 顾客菜单 =====");
            System.out.println("1. 查看所有宠物");
            System.out.println("2. 购买宠物");
            System.out.println("0. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除缓冲区

            switch (choice) {
                case 1:
                    getAllPets();

                    break;
                case 2:
                    System.out.print("请输入要购买的宠物ID: ");
                    int petId = scanner.nextInt();
                    scanner.nextLine(); // 清除缓冲区
                    System.out.print("请输入花费的积分: ");
                    int cost = scanner.nextInt();
                    scanner.nextLine(); // 清除缓冲区
                    buyPet(userId, petId, cost);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    public static User getUserByUsername(String username) {
        // 定义 SQL 查询语句
        String sql = "SELECT * FROM user WHERE username = ?";

        // 使用 JBDCUtils.query 方法查询用户
        List<User> userList = JBDCUtils.query(User.class, sql, username);

        // 如果查询结果不为空，返回第一个用户对象
        if (userList != null && !userList.isEmpty()) {
            return userList.get(0);
        }

        // 如果未找到用户，返回 null
        return null;
    }


    public static int addPet(int userId, String petName, String petType, int age, String gender) {
        String sql;
        int rows;
        if (userId == 0) {
            // 如果 user_id 为 0，表示宠物没有主人，设置为 NULL
            sql = "INSERT INTO pet (user_id, petname, pet_type, age, gender) VALUES (NULL, ?, ?, ?, ?)";
            rows = JBDCUtils.update(sql, petName, petType, age, gender);

        } else {
            // 否则，插入指定的 user_id
            sql = "INSERT INTO pet (user_id, petname, pet_type, age, gender) VALUES (?, ?, ?, ?, ?)";
             rows= JBDCUtils.update(sql, userId, petName, petType, age, gender);
        }
        if (rows > 0) {
            System.out.println("宠物添加成功！");
        } else {
            System.out.println("宠物添加失败！");
        }
        return rows;


    }

    // 更新宠物
    public static int updatePet(int petId, String petName, String petType, int age, String gender) {
        String sql = "UPDATE pet SET petname = ?, pet_type = ?, age = ?, gender = ? WHERE id = ?";
        int rows= JBDCUtils.update(sql, petName, petType, age, gender, petId);
        if (rows > 0) {
            System.out.println("宠物更新成功！");
        } else {
            System.out.println("宠物更新失败！");
        }
        return rows;

    }

    // 删除宠物
    public static int deletePet(int petId) {
        String sql = "DELETE FROM pet WHERE id = ?";
      int rows= JBDCUtils.update(sql, petId);
        if (rows > 0) {
            System.out.println("宠物删除成功！");
        } else {
            System.out.println("宠物删除失败！");
        }
        return rows;
    }

    // 查看所有宠物
    public static void getAllPets() {
        List<Pet> pets = JBDCUtils.query(Pet.class, "select*from pet");
        for (Pet pet : pets) {
            System.out.println(pet);
        }
    }
    // 购买宠物
    public static int buyPet(int userId, int petId, int cost) {
        String sql1 = "UPDATE pet SET user_id = ? WHERE id = ?";
        String sql2 = "UPDATE user SET score = score - ? WHERE id = ?";
        try (Connection conn = DataSourceUtils.getConnection()) {
            conn.setAutoCommit(false);
            JBDCUtils.update(sql1, userId, petId);
            JBDCUtils.update(sql2, cost, userId);
            conn.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}

